package com.park.common.file;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.park.common.file.FileUtil;

public class ImageUtil {
	private static final Log log = LogFactory.getLog(ImageUtil.class);

	private ImageUtil(){}
	
	/**
	 * 读取默认图像图片
	 */
	public static  byte[] getImageBytes(){
		InputStream is = ImageUtil.class.getResourceAsStream("/default.png");
		return FileUtil.getBytes(is);
	}
	
	public static void write(byte[] data,OutputStream os){
		InputStream is = new ByteArrayInputStream(data);
		int len = 0;
		byte[] b = new byte[1024];
		try {
			while((len = is.read(b)) != -1){
				os.write(b, 0, len);
			}
			os.close();
			is.close();
		} catch (IOException e) {
			log.error(e);
		}
	}
	
	public static void writeToPicture(byte[] data,HttpServletResponse response) throws IOException{
		response.setContentType("image/jpg");
		response.setCharacterEncoding("utf-8");
		OutputStream os = response.getOutputStream();
		write(data, os);
	}
	
	/**
	 * 显示图片使用，根据路径
	 */
	public static void showPicture(String path,OutputStream os) throws IOException{
		byte[] data = FileUtil.getBytes(path);
		write(data,os);
	}
}
